package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.BaseDepartDao;
import ysq.xyg.dao.BaseMenuDao;
import ysq.xyg.model.BaseMenu;
import ysq.xyg.service.BaseMenuService;
import ysq.xyg.util.BaseConstant;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Yu Shengqiang on 2017/5/3.
 */
@Service("baseMenuService")
public class  BaseMenuServiceImpl extends BaseServiceImpl<BaseMenu> implements BaseMenuService {

    @Resource
    private BaseMenuDao baseMenuDao;
    @Autowired
    public void setBaseMenuDao(BaseMenuDao baseMenuDao) {
        this.baseDao = baseMenuDao;
    }
    //分页查询
    public PageInfo<BaseMenu> pageInfo(BaseMenu baseMenu, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("menunum desc");
        //通用Example查询
        Example example = new Example(BaseMenu.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(baseMenu.getMenuname())){
            criteria.andLike("menuname","%"+baseMenu.getMenuname()+"%");
        }
        if(StringUtil.isNotEmpty(baseMenu.getRemovetag())){
            criteria.andEqualTo("removetag",baseMenu.getRemovetag());
        }
        List<BaseMenu> list=baseMenuDao.selectByExample(example);
        return new PageInfo<BaseMenu>(list);
    }

    //获取最大menuCode值
    public String AcceptMaxMenuCode(BaseMenu baseMenu){
        return baseMenuDao.AcceptMaxMenuCode(baseMenu);
    }
    //获取最大menuCode值
    public String AcceptMaxMenuCode(String menuCode){
        BaseMenu baseMenu=new BaseMenu();
        if (StringUtil.isNotEmpty(menuCode)){
            baseMenu.setMenucode(menuCode);
        }
        baseMenu.setRemovetag(BaseConstant.REMOVE_N);
        baseMenu.setMenugrade(BaseConstant.MENU_ONE);
        String menuCodeMax =baseMenuDao.AcceptMaxMenuCode(baseMenu);

        if(StringUtil.isEmpty(menuCodeMax)){
            menuCodeMax=BaseConstant.MENU_CODE_MIN;
        }else{
            menuCodeMax=Long.parseLong(menuCodeMax)+1+"";
        }
        return menuCodeMax;
    }

    /***
     * 菜单左查询
     * @param baseMenu
     * @return
     */
    public List<BaseMenu> selectEntityByLeft(BaseMenu baseMenu){
        return baseMenuDao.selectEntityByLeft(baseMenu);
    }
    //获取最大menuCode值
    public String AcceptMaxMenuCode(String parentId,String menuGrade){
        BaseMenu baseMenu=new BaseMenu();
        baseMenu.setParentmenuid(parentId);
        baseMenu.setMenugrade(menuGrade);
        baseMenu.setRemovetag(BaseConstant.REMOVE_N);
        String menuCodeMax =baseMenuDao.AcceptMaxMenuCode(baseMenu);
        if(StringUtil.isEmpty(menuCodeMax)){
            menuCodeMax=BaseConstant.MENU_CODE_MIN;
        }else{
            menuCodeMax=Long.parseLong(menuCodeMax)+1+"";
        }
        return menuCodeMax;
    }
}
